angular.module('furion.filters', [])

.filter 'DateTimeToSecondsQuan', ()->
  return (value)->
    return "无时间限制" if 0 == value
    mp = new Date(value)
    return "#{mp.getFullYear()}/#{mp.getMonth()+1}/#{mp.getDate()}"# #{mp.getHours()}:#{mp.getMinutes()}:#{mp.getSeconds()}"

.filter 'Numberic', ()->
  return (value, digit=2)->
    value = 0 if !value
    value = parseFloat(value)
    if (value / 100000000.0) >= 1
      return furion.utils.NumberFloor((value / 100000000.0), digit)
    else if (value / 10000.0) >= 1
      return furion.utils.NumberFloor((value / 10000.0), digit)
    else
      return furion.utils.NumberFloor(value, digit)

.filter 'NumbericUnit', ()->
  return (value)->
    value = parseFloat(value)
    if (value / 100000000) >= 1
      return '亿'
    else if (value / 10000) >= 1
      return '万'
    else
      return ''

.filter 'NumbericAndUnit', ()->
  return (value, digit)->
    value = parseFloat(value)
    if (value / 100000000.0) >= 1
      return "#{furion.utils.NumberFloor((value / 100000000.0), digit)}亿"
    else if (value / 10000.0) >= 1
      return "#{furion.utils.NumberFloor((value / 10000.0), digit)}万"
    else
      return "#{furion.utils.NumberFloor(value, digit)}"

.filter 'NumberFloor', ()->
  return (value, digit=2)->
    v = if value then value.toString() else '0'
    pos_decimal = v.indexOf('.')
    if pos_decimal < 0
      pos_decimal = v.length
      v += ".0"
    while v.length <= pos_decimal + digit
      v += '0'
    len = pos_decimal + digit + 1
    len -= 1 if digit <= 0
    return v.substr(0,  len)

.filter 'phoneFilter', ()->
  return (value)->
    if value.length <= 3
      return value
    if value.length > 3 and value.length <= 7
      return "#{value[0..2]} #{value[3..(value.length-1)]}"
    if value.length > 7
      return "#{value[0..2]} #{value[3..6]} #{value[7..(value.length-1)]}"

.filter 'FilterPhone', ()->
  return (value)->
    if value.length <= 11
      return value
    # if value.length > 3 and value.length <= 7
    #   return "#{value[0..2]} #{value[3..(value.length-1)]}"
    # if value.length > 7
    #   return "#{value[0..2]} #{value[3..6]} #{value[7..(value.length-1)]}"
    if value.length > 11
      value = value.replace(/\-/g, '')
      return value.slice(-11)

.filter 'to_trusted', ($sce)->
  return (value)->
    return $sce.trustAsHtml(value);

.filter 'phoneHide', ()->
  return (value)->
    return value if !value || value.length < 11
    return "#{value[0..2]}****#{value[7..(value.length-1)]}"

.filter 'distanceLimit', ()->
  return (value)->
    return value if !value || value.length < 4
    if value.length > 4
      return value.slice(0, -3) + '.' + value.slice(-3, -1)


